/*
 * title: vue页面共用参数、方法
 * date: 2019-05-15
 * author: wjh
 */

export default {
  props: {
    // id
    id: {
      type: String,
      required: false
    },
    // visible
    visible: {
      type: Boolean,
      required: false
    },
    // screen
    screen: {
      type: Object,
      required: false
    }
  },
  computed: {},
  data() {
    return {
      userList: [], // 账户列表
      searchDate: [], // 日期数组，多用于时间段
      form: {}, // 表单
      drawerVisible: false, // drawer 是否显示
      formLoading: false, // 表单加载loading
      loading: false // loading
    }
  },
  created() {
    (this.screen && this.screen.form && this.screen.form.startDate && this.screen.form.endDate) && (this.searchDate = [this.screen.form.startDate, this.screen.form.endDate])
    setTimeout(() => {
      this.drawerVisible = this.visible
    }, 10)
  },
  methods: {
    // 获取账户
    async $getUserList () {
      const result = await this.$getData('manage/system/findAccount', { pageNum: 1, pageSize: 20 })
      if (result && result.list.length) {
        this.userList = result.list
      }
    },
    // 获取详情(调用服务器接口)
    async $getDetail(path, params) {
      if (!params && !this.id) {
        return
      }
      params = params || { id: this.id }
      this.formLoading = true
      const result = await this.$getData(path, params)
      this.formLoading = false
      if (result) {
        this.form = result || {}
      }
    },
    // 保存(调用服务器接口), type有两个值(false, freshName), type为false直接返回code
    async $save(path, type) {
      // 表单验证
      const status = this.$formValidate()
      if (status) {
        this.loading = true
        const code = await this.$postCode(path, this.form)
        this.loading = false
        if (type === false) {
          return code
        }
        if (code === true) {
          this.$close(true, type)
        }
      }
    },
    // 下载
    $download(url) {
      window.location.href = url
    },
    // 返回
    $close(fresh, freshName) {
      if (fresh === true) {
        this.$emit(`global:${freshName}`)
      }
      this.drawerVisible = false
      setTimeout(() => {
        this.$emit('update:visible', false)
        }, 300)
    },
    // 表单校验
    $formValidate() {
      let status
      this.$refs.form.validate(valid => {
        status = valid
      })
      return status
    }
  }
}
